package edu.ucsb.cs56.W12.chatclient.controller;

import java.util.ArrayList;

import edu.ucsb.cs56.W12.chatclient.model.Client;
import edu.ucsb.cs56.W12.chatclient.model.Contact;
import edu.ucsb.cs56.W12.chatclient.view.ClientWindow;
/**
 * ClientController class controlls the communication between client view and client model
 * @author PengWang
 * @version 0.1
 */
public class ClientController {
	private Client client;
	private static ClientController controller;
	
	/**
	 * Constructor saves a instance of a client object
	 */
	private ClientController(){
		client = Client.getClient();
	}
	
	/**
	 * get controller instance
	 * @return instance of ClientController
	 */
	public static ClientController getController(){
		if(controller == null)
			controller = new ClientController();
		return controller;
	}

	/**
	 * Connect to the server
	 * @param ip Server IP address
	 * @param name username
	 */
	public void connectServer(String ip, String name){
		client.connectServer(ip, name);
	}
	
	/**
	 * display message on the client window
	 * @param msg message to display on client window
	 */
	public void displayMsg(String msg){
		ClientWindow.getWindow().getTaOutput().append(msg);
	}
	
	/**
	 * send a message to the server
	 * @param s message to send to the server
	 */
	public void sendMsg2Server(String s){
		client.sendMsg(s);
	}
	
	/**
	 * create and return the contact list
	 * @return contact names
	 */
	public String[] getContacts(){
		Contact[] contacts = new Contact[5];
		String[] names = new String[5];
		for(int i = 0; i < 5; i++){
			contacts[i] = new Contact("contact" + i);
			names[i] = contacts[i].getName();
		}
		return names;
	}
}
